package com.uwosh.cs342.g6.mapapp;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class Viewfriend extends Activity {

	final String KEY_LAT = "lat";
	final String KEY_LON = "lon";
	int FID = -1;
	int Findy = -1;
	final String deleteURL = "http://softeng.cs.uwosh.edu/students/cs342g6/deleteFriend.php?id=";// XML
	final String findURL = "http://softeng.cs.uwosh.edu/students/cs342g6/getFriends.php?loginid="+Login.currentUserId+"&id="; // XML node keys
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_viewfriend);
		Intent i = getIntent();
		FID = Integer.parseInt(i.getStringExtra("id"));
		Findy = Integer.parseInt(i.getStringExtra("read"));

		findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					String g = new FriendLocationRequests().execute(Findy).get();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// call async task
				
				


				// call stalking php code to get location launch map and put
				// marker on map
			}
		});

		findViewById(R.id.Delete).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						deletefriendship(FID);
					}
				});

		findViewById(R.id.cancel).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						close();
					}
				});
	}

	public void deletefriendship(int ID) {
		try {
			String x = new deleteFriendRequests().execute(ID).get();
		} catch (InterruptedException e) {
			//
			e.printStackTrace();
		} catch (ExecutionException e) {
			//
			e.printStackTrace();
		}
		this.finish();
	}

	public class deleteFriendRequests extends
			AsyncTask<Integer, Context, String> {

		@Override
		protected String doInBackground(Integer... params) {

			int userNum = Login.currentUserId;
			String response = "";

			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(deleteURL + params[0]);
			try {

				// Execute HTTP Post Request

				ResponseHandler<String> responseHandler = new BasicResponseHandler();
				response = httpclient.execute(httppost, responseHandler);
				response = response.trim();
				int temp = Integer.parseInt(response);
				if (temp == 1) {
					return "success";
				} else {
					return "failure";
				}
			} catch (ClientProtocolException e) {
			} catch (IOException e) {
			}
			return null;
		}

		@Override
		protected void onPostExecute(String values) {

			if (values.equals("success")) {
				Context context = getApplicationContext();
				Toast.makeText(context, "deleted", Toast.LENGTH_SHORT).show();

			}

		}
	}

	private void close() {
		this.finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.viewfriend, menu);
		return true;
	}

	public class FriendLocationRequests extends
			AsyncTask<Integer, Context, String> {

		@Override
		protected String doInBackground(Integer... params) {

			//int userNum = Login.currentUserId;
			//int 
			String response = "";

			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(findURL + params[0]);
			try {

				// Execute HTTP Post Request

				ResponseHandler<String> responseHandler = new BasicResponseHandler();
				response = httpclient.execute(httppost, responseHandler);
				response = response.trim();
				Log.d("here123", response+"");
				return response;
			} catch (ClientProtocolException e) {
			} catch (IOException e) {
			}
			return null;
		}

		@Override
		protected void onPostExecute(String values) {

			String[] temp = new String[2];
			temp = values.split(":");
			double lat = Double.parseDouble(temp[0]);
			double lon = Double.parseDouble(temp[1]);
			Intent in = new Intent(getApplicationContext(), MainActivity.class);
			Log.d("here", lat+" "+lon+" ");
			in.putExtra("latf", lat+"");
			in.putExtra("lonf", lon+"");
			finish();
			startActivity(in);
			
		}
	}

}
